#include "Grenade.h"
#include "RSManager.h"
#include "Box.h"
#include "Sprite.h"


Grenade::Grenade(SPRITE_ID id) : Object(new Box(D3DXVECTOR2(), D3DXVECTOR2(), D3DXVECTOR2()), ObjEnemy_DYNAMID), MoveObject()
{	
	_explode = RSManager::getInstance()->getSprite(id);
	_bullet = RSManager::getInstance()->getSprite(GRENADE);
	AssignMove(_box);	
	_id = id;
}


Grenade::~Grenade(void)
{
}

void Grenade::process(ObjectType oType, D3DXVECTOR2 pos, float v, float angle)
{
	_isAlive = true;	
	_timeExplode = 0;
	_angle = angle;	
	_box->_position = pos;	
	_box->_v.x = _box->_v.y = v;
	_sprite = _bullet;
	_box->_size = _sprite->getRenderSize();	
	delay = 0;
}

void Grenade::update(float groundLevel)
{
	
	if(_box->_position.y >= groundLevel)
		moveDiagonalThrow(_angle);
	else
	{
		delay += GAME_TIME;
		_sprite = _explode;
		if(delay >= 150)
		{
			delay = 0;
			_sprite->Next();
			if(_sprite->GetCurrentIndex() == 0)
				_isAlive = false;
		}
	}	
}

void Grenade::render()
{
	_sprite->Render(_box->_position);
}

bool Grenade::isAlive()
{
	if (!_isAlive)
	{
		if(_timeExplode >= 250)			
			return false;
		_timeExplode += GAME_TIME;
	}
	return true;	
}

Box* Grenade::getCollisionBox()
{
	return _box;
}

